package com.tj.common;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

public class OSHelper {
    /**
     * 
     * Runtime.getRuntime().exec( new String[] { "cmd.exe", "/E:1900", "/C",
     * "MyBat.bat"}, new String[] { "flavour=strawberry", "font=Stone"},
     * "C:\\SomeDirectory" );
     * 
     * @param command
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static String executeCommandForString(String command, File dir) throws IOException, InterruptedException {
        String lsString = null;
        Process process = null;
        StringBuffer buf = new StringBuffer();
        process = Runtime.getRuntime().exec(command, null, dir);
        Reader reader = new InputStreamReader(process.getInputStream());
        char[] cbuf = new char[1024];
        int len = reader.read(cbuf);
        while (len != -1) {
            buf.append(cbuf, 0, len);
            len = reader.read(cbuf);
        }
        process.waitFor();
        if (buf.length() == 0) {
            reader = new InputStreamReader(process.getErrorStream());
            len = reader.read(cbuf);
            while (len != -1) {
                buf.append(cbuf, 0, len);
                len = reader.read(cbuf);
            }
        }
        return buf.toString();
    }

    public static boolean isLinux() {
        return System.getProperty("os.name").toLowerCase().indexOf("linux") != -1;
    }

    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().indexOf("windows") != -1;
    }

    public static String getOsName() {
        return System.getProperty("os.name");
    }

    public static void main(String[] args) throws Exception {
        // System.out.println(executeCommandForString("ls /usr/"));
        System.out.println(System.getProperty("os.name"));
    }
}
